package com.example.toutiao.ui.main.act

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.example.toutiao.R
import com.example.toutiao.base.BaseActivity
import com.example.toutiao.base.Constants
import com.example.toutiao.base.Constants.MOBILE
import com.example.toutiao.ui.main.model.RegisterModel
import com.example.toutiao.ui.main.presenter.RegisterPresenter
import com.example.toutiao.ui.main.view.RegisterView
import com.example.toutiao.ui.user.EvtMsgModel
import com.example.toutiao.utils.CommonUtils
import com.example.toutiao.view.MyCheckBox
import kotlinx.android.synthetic.main.activity_register.*

class RegisterActivity : BaseActivity<RegisterView, RegisterPresenter>(),RegisterView, TextWatcher {

    private var phoneText:String =""
    override fun getLayoutId(): Int =R.layout.activity_register

    override fun initView() {

        cd_next.setCardBackgroundColor(ContextCompat.getColor(this,R.color.c_222222))
        met_mobile.addTextChangedListener(this)
        icon_back.setOnClickListener {
            finish()
        }
        my_checkbox.setChecked(false)
        my_checkbox.isShow(true)
        my_checkbox.initSpan(R.string.xy)
        my_checkbox.setOnClickListener {
            my_checkbox.changeState()
            checkPhone()
        }
        my_checkbox.setClickableSpanText(object : MyCheckBox.ClickableSpanText{
            override fun onClick(type: Int) {
                if(type==1){
                    showToast("用户协议")
                }else{
                    showToast("隐私协议")
                }
            }
        })
        cd_next.setOnClickListener {
            if(TextUtils.isEmpty(phoneText)){
                Toast.makeText(application,"请先填写电话号码", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if(!my_checkbox.isChecked()){
                Toast.makeText(application,"请先勾选协议", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val intent=Intent(this@RegisterActivity,SendCodeActivity::class.java)
            intent.putExtra(MOBILE,phoneText)
            intent.putExtra(Constants.USER_ACTION, Constants.ACTION_REGISTER)
            startActivity(intent)
        }
    }

    override fun initData() {

    }

    override fun createPresenter(): RegisterPresenter =RegisterPresenter()
    override fun onRegister(registerModel: RegisterModel) {

    }

    override fun <T> setData(data: T) {

    }

    override fun setError(err: String) {

    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        phoneText=s.toString()
        checkPhone()
    }
    private fun checkPhone(){
        if(CommonUtils.replaceBlank(phoneText).length==11&&my_checkbox.isChecked()){
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this,R.color.main_color))
            cd_next.isEnabled=true
        }else{
            cd_next.setCardBackgroundColor(ContextCompat.getColor(this,R.color.c_222222))
            cd_next.isEnabled=false
        }
    }

    override fun afterTextChanged(s: Editable?) {

    }

    override fun isRegister(): Boolean =true
    override fun getMessage(message: EvtMsgModel<*>) {
        super.getMessage(message)
        if(message!=null){
            if(message.code==101){
                finish()
            }
        }
    }
}